/**
 * 
 */
package com.antilia.infomaresme.provider;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.data.dao.interfaces.IItemsDao;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.User;
import com.antilia.infomaresme.data.dataobjects.VisibilityStatus;

/**
 * @author Ernesto
 *
 */
public abstract class ItemsDataProvider extends SortableDataProvider<Item>  implements ISortableDataProvider<Item> {

	private static final long serialVersionUID = 1L;
		
	/**
	 * 
	 */
	public ItemsDataProvider() {
		
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.model.IDetachable#detach()
	 */
	public void detach() {

	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
	 */
	public Iterator<? extends Item> iterator(int first, int count) {
		Boolean isAscending = null;
		Boolean isRatingAscending =  null;
		if(getSort() != null && getSort().getProperty() != null && getSort().getProperty().equals("price")) {
			isAscending = getSort().getProperty().equals("price") && getSort().isAscending();
		} else if(getSort() != null && getSort().getProperty() != null && getSort().getProperty().equals("rating"))
			isRatingAscending = getSort().isAscending();
		return getIItemsDao().getItems(getUser(), getCategory(), getPlace(), first, count, isAscending, isRatingAscending, getItemStatus()).iterator();
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		return getIItemsDao().countItems(getUser(), getCategory(), getPlace(), getItemStatus()).intValue();
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<Item> model(Item object) {
		return new ItemsLdm(object);
	}

	protected final IItemsDao getIItemsDao() {
		return InfomaresmeApplication.getQAApplication().getiItemsDao();
	}
	
	public abstract Category getCategory();


	public abstract Category getPlace();
	
	protected abstract VisibilityStatus getItemStatus();
	
	public abstract User getUser();
}
